home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / MotifApp / DialogManager.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.7 KB  |  209 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////////////
  22. // DialogManager.C: Support cached dialog widgets
  23. //////////////////////////////////////////////////////////
  24. #include "DialogManager.h"
  25. #include "Application.h"
  26. #include <Xm/MessageB.h>
  27. #include <assert.h>
  28.  
  29. DialogManager::DialogManager ( char   *name ): UIComponent ( name )
  30. {
  31.     // Empty
  32. }
  33.  
  34. Widget DialogManager::getDialog()
  35. {
  36.     Widget newDialog = NULL;
  37.     
  38.     // If the permanent widget exists and is not in use,
  39.     // just return it
  40.     
  41.     if ( _w && !XtIsManaged ( _w ) )
  42.     return _w;
  43.     
  44.     // Get a widget from the derived class
  45.     
  46.     newDialog = createDialog ( theApplication->baseWidget() ) ;
  47.     
  48.     // If this is a temporary dialog, install callbacks to 
  49.     // destroy it when the user pops it down.
  50.     
  51.     if ( _w )
  52.     {
  53.     XtAddCallback ( newDialog, 
  54.                XmNokCallback, 
  55.                &DialogManager::destroyTmpDialogCallback,
  56.                (XtPointer) this );
  57.     
  58.     XtAddCallback ( newDialog, 
  59.                XmNcancelCallback, 
  60.                &DialogManager::destroyTmpDialogCallback,
  61.                (XtPointer) this );
  62.     }
  63.     else                 // If this is the first dialog to be 
  64.     _w = newDialog;  // created, save it to be used again.
  65.     
  66.     return newDialog;
  67. }
  68.  
  69. void DialogManager::destroyTmpDialogCallback ( Widget     w, 
  70.                           XtPointer,
  71.                           XtPointer )
  72. {
  73.     XtDestroyWidget ( w );
  74. }
  75.  
  76. Widget DialogManager::post ( char          *text,
  77.                 void          *clientData,
  78.                 DialogCallback ok,
  79.                 DialogCallback cancel,
  80.                 DialogCallback help)
  81. {
  82.     // Get a dialog widget from the cache
  83.     
  84.     Widget dialog = getDialog();
  85.     
  86.     // Make sure the dialog exists, and that it is an XmMessageBox
  87.     // or subclass, since the callbacks assume this widget type
  88.     
  89.     assert ( dialog );
  90.     assert ( XtIsSubclass ( dialog, xmMessageBoxWidgetClass ) );
  91.     
  92.     // Convert the text string to a compound string and 
  93.     // specify this to be the message displayed in the dialog.
  94.     
  95.     XmString xmstr = XmStringCreateSimple ( text ); 
  96.     XtVaSetValues ( dialog, XmNmessageString, xmstr, NULL );
  97.     XmStringFree ( xmstr );
  98.     
  99.     // Create an object to carry the additional data needed
  100.     // to cache the dialogs.
  101.     
  102.     DialogCallbackData *dcb = new DialogCallbackData( this, 
  103.                              clientData,
  104.                              ok, cancel, 
  105.                              help );
  106.     // Install callback function for each button 
  107.     // support by Motif dialogs. If there is no help callback
  108.     // unmanage the corresponding button instead, if possible.
  109.     
  110.     XtAddCallback ( dialog, 
  111.            XmNokCallback, 
  112.            &DialogManager::okCallback,
  113.            (XtPointer) dcb );
  114.     
  115.     XtAddCallback ( dialog, 
  116.            XmNcancelCallback, 
  117.            &DialogManager::cancelCallback,
  118.            (XtPointer) dcb );
  119.     
  120.     if ( help )        
  121.     XtAddCallback ( dialog, 
  122.                XmNhelpCallback, 
  123.                &DialogManager::helpCallback,
  124.                (XtPointer) dcb );
  125.     else
  126.     {
  127.     Widget w = XmMessageBoxGetChild ( dialog,
  128.                      XmDIALOG_HELP_BUTTON );
  129.         XtUnmanageChild ( w );
  130.     }
  131.     
  132.     // Post the dialog.
  133.     
  134.     XtManageChild ( dialog );
  135.     
  136.     return dialog;
  137. }
  138.  
  139. void DialogManager::okCallback ( Widget    w, 
  140.                 XtPointer clientData,
  141.                 XtPointer )
  142. {
  143.     DialogCallbackData *dcd = (DialogCallbackData *) clientData;
  144.     DialogManager      *obj = (DialogManager *) dcd->dialogManager();
  145.     DialogCallback      callback;
  146.     
  147.     // If caller specified an ok callback, call the function
  148.     
  149.     if ( ( callback = dcd->ok() ) != NULL )
  150.     ( *callback )( dcd->clientData() );
  151.     
  152.     // Reset for the next time
  153.     
  154.     obj->cleanup ( w, dcd );
  155. }
  156.  
  157. void DialogManager::cancelCallback ( Widget    w, 
  158.                     XtPointer clientData,
  159.                     XtPointer )
  160. {
  161.     DialogCallbackData *dcd = (DialogCallbackData *) clientData;
  162.     DialogManager      *obj = (DialogManager *) dcd->dialogManager();
  163.     DialogCallback      callback;
  164.     
  165.     if ( ( callback = dcd->cancel() ) != NULL )
  166.     ( *callback )( dcd->clientData() );
  167.     
  168.     obj->cleanup ( w, dcd );
  169. }
  170.  
  171. void DialogManager::helpCallback ( Widget    w, 
  172.                   XtPointer clientData,
  173.                   XtPointer )
  174. {
  175.     DialogCallbackData *dcd = (DialogCallbackData *) clientData;
  176.     DialogManager      *obj = (DialogManager *) dcd->dialogManager();
  177.     DialogCallback      callback;
  178.     
  179.     if ( ( callback = dcd->help() ) != NULL )
  180.     ( *callback )( dcd->clientData() );
  181.     
  182.     obj->cleanup ( w, dcd );
  183. }
  184.  
  185. void DialogManager::cleanup ( Widget w, DialogCallbackData *dcd )
  186. {
  187.     // Remove all callbacks to avoid having duplicate 
  188.     // callback functions installed.
  189.     
  190.     XtRemoveCallback ( w, 
  191.               XmNokCallback, 
  192.               &DialogManager::okCallback,
  193.               (XtPointer) dcd );
  194.     
  195.     XtRemoveCallback ( w, 
  196.               XmNcancelCallback, 
  197.               &DialogManager::cancelCallback,
  198.               (XtPointer) dcd );
  199.     
  200.     XtRemoveCallback ( w, 
  201.               XmNhelpCallback, 
  202.               &DialogManager::helpCallback,
  203.               (XtPointer) dcd );
  204.     
  205.     // Delete the DialogCallbackData instance for this posting
  206.     
  207.     delete dcd;
  208. }
  209.